/**
    Die Ewigen Extension
    Copyright (C) 2011 DonKrieg @ sonkyu@googlemail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

(function($) {
    $.ewigen = function() {
    }

    $.fn.ewigen = function() {
    }

    /** overview class */
    $.ewigen.overview = function() {
    }

    /** read the main player from the overview page */
    $.ewigen.overview.playerconfig = function(data) {
        for(var i=0; i<data.length; i++) {
            if(
                    data[i].getAttribute("class") == "cell"
                ||  data[i].getAttribute("class") == "cell1"
            ) {
                var mod = data[i].innerHTML;

                switch(mod) {
                    case "Name": {
                        // save the data
                        $.ewigen.util.CS2BG(
                            {
                                action:"storeData",
                                key: "sde_name",
                                value: data[++i].innerHTML
                            }
                        );
                        localStorage["sde_name"] = data[i].innerHTML;
                        break;
                    }
                    case "Allianz": {
                        // save the data
                        $.ewigen.util.CS2BG(
                            {
                                action:"storeData",
                                key: "sde_alliance",
                                value: data[++i].innerHTML
                            }
                        );
                        break;
                    }
                    case "Kollektoren": {
                        // save the data
                        $.ewigen.util.CS2BG(
                            {
                                action:"storeData",
                                key: "sde_collectors",
                                value: $.ewigen.util.pointint2int(data[++i].innerHTML)
                            }
                        );
                        localStorage["sde_collectors"] = $.ewigen.util.pointint2int(data[i].innerHTML);
                        break;
                    }
                    case "Agenten": {
                        // save the data
                        $.ewigen.util.CS2BG(
                            {
                                action:"storeData",
                                key: "sde_agents",
                                value: data[++i].innerHTML
                            }
                        );
                        break;
                    }
                    case "System": {
                        // save the data
                        $.ewigen.util.CS2BG(
                            {
                                action:"storeData",
                                key: "sde_system",
                                value: data[++i].innerHTML
                            }
                        );
                        break;
                    }
                    case "Rasse": {
                        // save the data
                        $.ewigen.util.CS2BG(
                            {
                                action:"storeData",
                                key: "sde_race",
                                value: data[++i].innerHTML
                            }
                        );

                        localStorage["sde_race"]        = data[i].innerHTML;
                        localStorage["sde_race_short"]  = data[i].innerHTML.slice(0, 1);
                        break;
                    }
                }
            } // if
        } // for
    }

    /** chat class */
    $.ewigen.chat = function() {  
    }

    /** make a valid url if there is any in the chat data */
    $.ewigen.chat.urlReplacer = function() {
        var lines = document.getElementsByTagName('font');
        for(var i=0; i<lines.length; i++) {
            var data = lines[i].innerText;
            // we found an URL
            if(data.indexOf("http://") >= 0) {
                var start   = data.indexOf("http://");
                var end     = data.indexOf(" ", start);
                if(end <= -1) {
                    end = data.length;
                }

                // extract url
                var url = data.substring(start, end);

                // build the final string
                lines[i].innerHTML  = data.substring(0, start);
                lines[i].innerHTML += "<a href='"+url+"' target='_blank'>"+url+"</a>";
                lines[i].innerHTML += data.substring(end, data.length);
            }
        }
    }

    /** retrieve the module name from the url */
    $.ewigen.getModule = function(url) {
        url = url.toString();
        var end = url.indexOf(".");
        var sub = url.substring(7, end);
        
        return sub;
    }

    /** localStorage loader class */
    $.ewigen.load = function() {
    }

    $.ewigen.load.text = function(key) {
        if(localStorage[key] != undefined) {
            return localStorage[key];
        }
        return undefined;
    }

    $.ewigen.load.bool = function(key) {
        if(localStorage[key] != undefined) {
            return localStorage[key];
        }
        return false;
    }

    /** localStorage saver class */
    $.ewigen.save = function() {
    }

    $.ewigen.save.bool = function(key, value) {
        localStorage[key] = value;
    }

    $.ewigen.save.text = function(key, value) {
        localStorage[key] = value;
    }

    /** util class providing some useful methods */
    $.ewigen.util = function() {
    }

    /**
     * converts an int to int with thousand points; 123454678 will convert to 12.345.678
     **/
    $.ewigen.util.int2pointint = function(val) {
        if(val == 0) {
            return 0;
        }

        val = val.toString()

        var num = "";
        var t = 0;
        if((t = (val.length % 3)) != 0) {
            num = val.substring(0, t);
            val = val.slice(t, val.length);
        }

        while(val.length > 0) {
            if(num.length > 0) {
                num += ".";
            }
            num +=  val.substring(0, 3);
            val =   val.slice(3, val.length);
        }

        return num;
    }

    /** converts an pointed int to int with thousand points 12.345.678 will convert to 12345678 */
    $.ewigen.util.pointint2int = function(val) {
        return val.replace(/\./g, "");
    }

    /**
     * method will send the data from the contentscript to the background page and recieve data from it
     *
     * @param data      JSON
     */
    $.ewigen.util.CS2BG = function(data) {
        chrome.extension.sendRequest(
            data
        );
    }

    /**
     * return the ship's data of a specific race
     */
    $.fn.getShipData = function(race, shipdata) {
        var data = new Array();
        for(var i=0; i<$.ewigen.config.racedata[race]["SHIPS"].length; i++) {
            data[i] = $.ewigen.config.racedata[race]["SHIPS"][i][shipdata];
        }
        return data;
    }

    $.ewigen.getShipID = function(race, shipname) {
        for(var i=0; i<$.ewigen.config.racedata[race]["SHIPS"].length; i++) {
            if(shipname == $.ewigen.config.racedata[race]["SHIPS"][i][$.ewigen.config.SHIP_NAME]) {
                return i;
            }
        }
        return -1;
    }
})(jQuery)

